package com.atguigu.blog.web;

import com.atguigu.blog.dto.response.Result;
import com.atguigu.blog.pojo.Video;
import com.atguigu.blog.pojo.VideoInfo;
import com.atguigu.blog.service.LogInfoService;
import com.atguigu.blog.service.UserService;
import com.atguigu.blog.service.VideoInfoService;
import com.atguigu.blog.util.IdWorker;
import com.atguigu.blog.util.UserInfoUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 阿里云视频点播
 * 1.获取视频播放地址
 * 2.获取视频播放凭证
 * 3.上传视频到视频点播服务
 */
@Controller
@RequestMapping("")
public class VodController {

    @Autowired
    private VideoInfoService videoInfoService;

    @Autowired
    private UserService userService;

    @Autowired
    private LogInfoService logInfoService;

    @Autowired
    private IdWorker idWorker;

    /**
     * 网站影视页面
     * @param map
     * @return
     */
    @GetMapping("/videoList")
    public String videoList(Map<String, Object> map, HttpServletRequest request) {
        QueryWrapper<VideoInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("video_name");
        queryWrapper.eq("status", 1);// 查询审核已通过的视频
        List<VideoInfo> videoInfoList = videoInfoService.list(queryWrapper);
        map.put("videoInfoList", videoInfoList);
        logInfoService.saveLogInfo(request, "视频页面");
        return "video/videoList";
    }

    /**
     * 审核
     * @param id
     * @param isTrue
     * @return
     */
    @GetMapping("/shen/{id}/{isTrue}")
    public String shen(@PathVariable("id") String id, @PathVariable("isTrue") Integer isTrue) {
        VideoInfo videoInfo = new VideoInfo();
        videoInfo.setId(id);
        videoInfo.setStatus(isTrue);
        videoInfoService.updateById(videoInfo);
        return "redirect:/ucenter";
    }

    @GetMapping("/del/{id}")
    public String del(@PathVariable("id") String id) {
        videoInfoService.removeById(id);
        return "redirect:/ucenter";
    }

    @RequestMapping("/createVideo")
    public String createVideo(Authentication authentication) {
        if (authentication==null) {
            return "redirect:/login";
        }
        return "video/createVideo";
    }

    @RequestMapping("/updateVideo/{id}")
    public String updateVideo(@PathVariable("id") String id, Map<String, Object> map, Authentication authentication) {
        if (authentication==null) {
            return "redirect:/login";
        }
        VideoInfo videoInfo = videoInfoService.getById(id);
        map.put("videoInfo", videoInfo);
        return "video/updateVideo";
    }

    @PostMapping("/updateVideoInfo")
    public String uploadPage(VideoInfo videoInfo, Map<String, Object> map) {
        videoInfoService.updateById(videoInfo);
        return "redirect:/ucenter";
    }

    @PostMapping("/saveVideo")
    public String save(VideoInfo videoInfo, Authentication authentication) {
        if (authentication==null) {
            return "redirect:/login";
        }
        String username = UserInfoUtil.getUsername(authentication);
        videoInfo.setCreateBy(username);
        videoInfo.setUpdateBy(username);
        videoInfo.setFavourCount("0");
        videoInfo.setTreadCount("0");
        videoInfo.setStatus(0);// 审核状态 0未审核 1审核通过 2审核未通过
        videoInfo.setUpdateDate(new Date());
        videoInfo.setCreateDate(new Date());
        videoInfoService.save(videoInfo);
        return "redirect:/ucenter";
    }

}
